package commonInfrastructure.menu.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.entity.WarehousePersonnelEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import commonInfrastructure.accountManagement.session.WarehousePersonnelSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class Menu2ManagedBean implements Serializable {

    private MenuModel model;
    @EJB
    EmployeeSessionBean employee;
    @EJB
    WarehousePersonnelSessionBean warehouseSession;

    public Menu2ManagedBean() {
        warehouseSession = new WarehousePersonnelSessionBean();
    }

    public MenuModel getModel() {
//            if(model == null)
//            {
        model = new DefaultMenuModel();

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Boolean isLogin = (Boolean) request.getSession().getAttribute("isLogin");


        if (isLogin == true) {
            Long loginId = (Long) request.getSession().getAttribute("userId");
            EmployeeEntity user = employee.getEmployeeById(loginId);
            List<String> userType = new ArrayList<String>();

            for (int i = 0; i < user.getRoles().size(); i++) {
                userType.add(user.getRoles().get(i).getRoleName());
            }

            //First submenu
            Submenu submenu = new Submenu();
            MenuItem item = new MenuItem();


            if (userType.contains("Salesman")) {

                submenu.setLabel("Client Management");
                submenu.setIcon("ui-icon ui-icon-flag");

                item.setValue("Register New Client");
                item.setUrl("/crm/clientManagement/createNewCompany.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Clients");
                item.setUrl("/crm/clientManagement/deleteCompany.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Inquiry");
                submenu.setIcon("ui-icon ui-icon-comment");

                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/crm/salesOrderProcessing/createSalesInquiry.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Inquiries");
                item.setUrl("/crm/salesOrderProcessing/deleteSalesInquiry.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Quotation");
                submenu.setIcon("ui-icon ui-icon-note");

                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/crm/salesOrderProcessing/createSalesQuotation.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Raise From Inquiry");
                item.setUrl("/crm/salesOrderProcessing/inquiryToQuotation.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Quotations");
                item.setUrl("/crm/salesOrderProcessing/deleteSalesQuotation.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Order");
                submenu.setIcon("ui-icon ui-icon-cart");

                item = new MenuItem();
                item.setValue("Raise New");
                item.setUrl("/crm/salesOrderProcessing/convertSalesQuotation.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View Pending Large Order");
                item.setUrl("/crm/salesOrderProcessing/viewPendingOrders.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View All Sales Order");
                item.setUrl("/crm/salesOrderProcessing/viewAllSalesOrder.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Promotion");
                submenu.setIcon("ui-icon ui-icon-star");

                item = new MenuItem();
                item.setValue("Manage Promotion");
                item.setUrl("/crm/salesOrderProcessing/managePromotion.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

            }

            if (userType.contains("Admin")) {

                submenu = new Submenu();
                submenu.setLabel("Account");
                submenu.setIcon("ui-icon ui-icon-contact");

                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddEmployee.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Employees");
                item.setUrl("/accountManagement/ManageEmployee.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Role");
                submenu.setIcon("ui-icon ui-icon-star");

                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddRole.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Roles");
                item.setUrl("/accountManagement/ManageRole.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Functionality");
                submenu.setIcon("ui-icon ui-icon-wrench");

                item = new MenuItem();
                item.setValue("Create New");
                item.setUrl("/accountManagement/AddFunctionality.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Functionality");
                item.setUrl("/accountManagement/ManageFunctionality.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("Audit Trail");
                submenu.setIcon("ui-icon ui-icon-note");

                item = new MenuItem();
                item.setValue("View Audit Trail");
                item.setUrl("/commonInfrastructure/system/viewAuditTrail.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("System Settings");
                submenu.setIcon("ui-icon ui-icon-gear");

                item = new MenuItem();
                item.setValue("Manage Settings");
                item.setUrl("/commonInfrastructure/system/systemSettings.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
            }

            if (userType.contains("Finance Personnel")) {

                submenu = new Submenu();
                submenu.setLabel("Invoice");
                submenu.setIcon("ui-icon ui-icon-document-b");

                item = new MenuItem();
                item.setValue("Create Invoice for Credit Sale");
                item.setUrl("/frm/billingAndPaymentProcess/createInvoice.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Create CIA Invoice");
                item.setUrl("/frm/billingAndPaymentProcess/createCIAInvoice.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View Invoice");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllInvoices.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Commence Dunning Email");
                item.setUrl("/frm/billingAndPaymentProcess/commenceDunningEmail.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Incoming Bill");
                submenu.setIcon("ui-icon ui-icon-note");

                item = new MenuItem();
                item.setValue("Create Record");
                item.setUrl("/frm/billingAndPaymentProcess/createIncomingBillRecord.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View Incoming Bills");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllIncomingBills.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View Vouchers");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllVouchers.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Receipt");
                submenu.setIcon("ui-icon ui-icon-document");

                item = new MenuItem();
                item.setValue("View Receipt");
                item.setUrl("/frm/billingAndPaymentProcess/viewAllReceipt.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Credit Control");
                submenu.setIcon("ui-icon ui-icon-bookmark");

                item = new MenuItem();
                item.setValue("View Credit Account");
                item.setUrl("/frm/creditControl/viewCreditAccount.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
            }

            if (userType.contains("Production Personnel")) {

                submenu = new Submenu();
                submenu.setLabel("Calendar");
                submenu.setIcon("ui-icon ui-icon-calendar");

                item = new MenuItem();
                item.setValue("Generate Calendar");
                item.setUrl("/mrp/generateCalendar.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Set Holidays");
                item.setUrl("/mrp/test.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("Sales Forecast");
                submenu.setIcon("ui-icon ui-icon-clock");

                item = new MenuItem();
                item.setValue("Create Forecast");
                item.setUrl("/mrp/createForecastTable.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Forecast Report");
                item.setUrl("/mrp/generateForecastReport.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);

                submenu = new Submenu();
                submenu.setLabel("MPS");
                submenu.setIcon("ui-icon ui-icon-wrench");

                item = new MenuItem();
                item.setValue("Create Monthly MPS");
                item.setUrl("/mrp/createMPSTable.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Create Daily Plan");
                item.setUrl("/mrp/createDailyMPSTable.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Monthly Schedule Report");
                item.setUrl("/mrp/generateMonthlyScheduleReport.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Daily Schedult Report");
                item.setUrl("/mrp/generateDailyScheduleReport.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
            }

            if (userType.contains("Warehouse Personnel")) {
                WarehousePersonnelEntity warehouseman = warehouseSession.getWarehousemanById(loginId);
                if (warehouseman.getWarehouseLevel().equals("Order Fulfillment Executive")) {
                    submenu = new Submenu();
                    submenu.setLabel("Generate Orders");
                    submenu.setIcon("ui-icon ui-icon-document");

                    item = new MenuItem();
                    item.setValue("Generate");
                    item.setUrl("/scm/inventoryControl/generateOrders.xhtml");
                    submenu.getChildren().add(item);

                    model.addSubmenu(submenu);
                } else {
                    submenu = new Submenu();
                    submenu.setLabel("Raw Material");
                    submenu.setIcon("ui-icon ui-icon-gear");

                    item = new MenuItem();
                    item.setValue("Create RM Batch Record");
                    item.setUrl("/scm/inventoryControl/createRMBatchRecord.xhtml");
                    submenu.getChildren().add(item);

                    item = new MenuItem();
                    item.setValue("View RM Batch Records");
                    item.setUrl("/scm/inventoryControl/viewRawMaterialBatchRecords.xhtml");
                    submenu.getChildren().add(item);

                    item = new MenuItem();
                    item.setValue("View RM Inventory");
                    item.setUrl("/scm/inventoryControl/viewRawMaterialInventory.xhtml");
                    submenu.getChildren().add(item);

                    item = new MenuItem();
                    item.setValue("Confirm Purchased RM Received");
                    item.setUrl("/scm/inventoryControl/confirmLineItemReceived.xhtml");
                    submenu.getChildren().add(item);

                    model.addSubmenu(submenu);

                    submenu = new Submenu();
                    submenu.setLabel("Final Goods");
                    submenu.setIcon("ui-icon ui-icon-cart");

                    item = new MenuItem();
                    item.setValue("Create FG Batch Record");
                    item.setUrl("/scm/inventoryControl/createProductBatchRecord.xhtml");
                    submenu.getChildren().add(item);


                    item = new MenuItem();
                    item.setValue("View FG Batch Records");
                    item.setUrl("/scm/inventoryControl/viewProductBatchRecords.xhtml");
                    submenu.getChildren().add(item);

                    item = new MenuItem();
                    item.setValue("View FG Inventory");
                    item.setUrl("/scm/inventoryControl/viewFinalGoodsInventory.xhtml");
                    submenu.getChildren().add(item);

                    item = new MenuItem();
                    item.setValue("Check Expiring FG");
                    item.setUrl("/scm/inventoryControl/checkPromotion.xhtml");
                    submenu.getChildren().add(item);

                    model.addSubmenu(submenu);
                }
            }
            
            if (userType.contains("RM Purchasing Personnel")) {

                submenu = new Submenu();
                submenu.setLabel("RM Purchase Order");
                submenu.setIcon("ui-icon ui-icon-document");

                item = new MenuItem();
                item.setValue("Create Order");
                item.setUrl("/scm/rawMaterialPurchasing/selectRawMaterialRequirement.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("View Order");
                item.setUrl("/scm/rawMaterialPurchasing/viesPurchasingOrders.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu); 
                
                submenu = new Submenu();
                submenu.setLabel("Supplier");
                submenu.setIcon("ui-icon ui-icon-transferthick-e-w");

                item = new MenuItem();
                item.setValue("Add Supplier");
                item.setUrl("/scm/rawMaterialPurchasing/createNewSupplier.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Manage Supplier");
                item.setUrl("/scm/rawMaterialPurchasing/manageSuppliers.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
                
                submenu = new Submenu();
                submenu.setLabel("BOM");
                submenu.setIcon("ui-icon ui-icon-note");

                item = new MenuItem();
                item.setValue("Manage BOM");
                item.setUrl("/scm/rawMaterialPurchasing/bomChooseAProduct.xhtml");
                submenu.getChildren().add(item);

                model.addSubmenu(submenu);
            }

        }
        return model;
    }
    //}
}
